﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class laoshi_pingfen : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            ddlKecheng.SelectedIndex = -1;
            Session["sTittle"] = "老师评分";
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (DataList3.Items.Count != 0)
        {
            getCom(6);
            ddlKecheng.SelectedIndex = -1;
        }
    }
    protected void ddlXuesheng_SelectedIndexChanged(object sender, EventArgs e)
    {
        getCom(3);
    }
    protected void btn_display_Click(object sender, EventArgs e)
    {
        DataList3.DataBind();
        if (ddlKecheng.SelectedIndex != -1 && ddlXuesheng.SelectedIndex != -1)
        {
            getCom(3);
        }
    }
    protected void getCom(int i)
    {
        string zkechengid = ddlKecheng.SelectedValue.ToString();
        Conn zconn = new Conn();
        string zsql = "";
        string zxtsj;
        switch (i)
        {

            //问题：如果问答题库里有两次考试的结果应该如何区分？--每次评分完毕，将finished字段赋值为1，初始值为0
            //从数据库中选择问答题
            case 3:
                zsql = "select * from tb_StuResult_wenda where finished=0 and kecheng_id="
                    + zkechengid + "and stu_username='" + ddlXuesheng.SelectedValue + "' order by id desc";
                DataSet myds3 = zconn.Conn_Send(zsql);
                DataList3.DataSource = myds3;
                DataList3.DataBind();
                //生成问答题题号，初始化验证控件
                for (int tID2 = 1; tID2 <= DataList3.Items.Count; tID2++)
                {
                    Label lblDSelect = (Label)DataList3.Items[tID2 - 1].FindControl("Labelwd"); //题号
                    Label lblScore0 = (Label)DataList3.Items[tID2 - 1].FindControl("lblWendaScore");//题目分数

                    RangeValidator RV = (RangeValidator)DataList3.Items[tID2 - 1].FindControl("RangeValidator1");//验证控件
                    //RV.MaximumValue = Int16.Parse(lblScore0.Text);//无法将类型“short”隐式转换为“string
                    RV.MaximumValue = lblScore0.Text;


                    lblDSelect.Text = tID2.ToString() + "、";
                }
                break;

            case 6: //把考试结果插入数据库
                {
                    int z_single, z_more, z_total;
                    string zcount;
                    string zpingjia = "";
                    int z_QA = 0;
                    for (int tID2 = 1; tID2 <= DataList3.Items.Count; tID2++)   //将所有评分记入数据表
                    {
                        zxtsj = DateTime.Now.ToString();
                        Label lblid = (Label)DataList3.Items[tID2 - 1].FindControl("lblid");   //本题目所在的记录ID.
                        TextBox txtScore = (TextBox)DataList3.Items[tID2 - 1].FindControl("txtScire1");//老师给的分数
                        z_QA += Int16.Parse(txtScore.Text); //用一个变量累计总分.这样一来,即使写入tb_StuResult_wenda时出错,也不会影响后续的写入tb_StuResult的问答题分数及计算总分的正确性
                        zsql = "update tb_StuResult_wenda set score1=" + txtScore.Text + ",finished=1,xtsj2='" + zxtsj + "' where id=" + lblid.Text;
                        zconn.ExeSql(zsql);
                    }
                    //以下计算该学生的问答题总分
                    zsql = "select res_single,res_more from tb_StuResult where kecheng_id=" + ddlKecheng.SelectedValue + " and stu_username='" + ddlXuesheng.SelectedValue + "' and res_total=999";
                    zcount = zconn.sqlTwo(zsql);
                    z_single = Int16.Parse(zcount.Substring(0, zcount.IndexOf("|")));
                    z_more = Int16.Parse(zcount.Substring(zcount.IndexOf("|") + 1));
                    z_total = z_single + z_more + z_QA;
                    if (z_total >= 0 && z_total < 10)
                    {
                        zpingjia = "一无所知";
                    }
                    if (z_total >= 10 && z_total < 30)
                    {
                        zpingjia = "未入门墙";
                    }
                    if (z_total >= 30 && z_total < 60)
                    {
                        zpingjia = "略知皮毛";
                    }
                    if (z_total >= 60 && z_total < 80)
                    {
                        zpingjia = "初窥门径";
                    }
                    if (z_total >= 80 && z_total < 100)
                    {
                        zpingjia = "颇有心得";
                    }
                    if (z_total == 100)
                    {
                        zpingjia = "登堂入室";
                    }


                    zsql = "update tb_StuResult set res_QA=" + z_QA.ToString() + ",res_total=" + z_total + ",pingjia='" + zpingjia + "' where kecheng_id=" + ddlKecheng.SelectedValue + "and stu_username='" + ddlXuesheng.SelectedValue + "' and res_total=999";
                    zconn.ExeSql(zsql);

                    if (z_total >= 60 && z_total <= 100)
                    {
                        string xsql = "select kecheng,kechenged from account where username='" + ddlXuesheng.SelectedValue + "'";
                        SqlDataReader Reader = DbHelper.ExecuteReader(CommandType.Text, xsql, null);

                        Reader.Read();
                        string kecheng = Reader.GetValue(0).ToString();
                        string kechenged = Reader.GetValue(1).ToString();
                        Reader.Close();

                        kechenged = kechenged + "," + ddlKecheng.SelectedValue + ",";
                        string[] ske = kecheng.Split(new char[] { ',' });
                        string nkecheng = ",";
                        foreach (string item in ske)
                        {
                            if (item != ddlKecheng.SelectedValue && item != "")
                            {
                                nkecheng += "," + item + ",";
                            }
                        }
                        string zzsql = "update account set [kechenged]='" + kechenged + "',[kecheng]='" + nkecheng + "' where [username]='" + ddlXuesheng.SelectedValue + "'";
                        zconn.ExeSql(zzsql);
                    }

                    DataList3.DataBind();
                }
                break;
        }
        zconn.Dispose();
    }



    protected void ddlKecheng_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataList3.DataBind();
    }
}
